const lodash = require("lodash");
const array = [
  {
    dicDataKey: 1,
    id: 323,
    value: "fds",
    isActive: true,
    isActiveDesc: "是",
    haveChild: true,
    userName: "admin",
    dateTimeDesc: "2022-02-22",
  },
  {
    dicDataKey: 1,
    id: 380,
    value: "fgfghhjj",
    isActive: true,
    isActiveDesc: "是",
    haveChild: true,
    userName: "admin",
    dateTimeDesc: "2022-02-23",
    children: [
      {
        dicDataKey: 1,
        id: 381,
        value: "gjhgjkhk",
        isActive: true,
        isActiveDesc: "是",
        haveChild: true,
        userName: "admin",
        dateTimeDesc: "2022-02-23",
      },
    ],
  },
];

function addChildren(arr, pid, children) {
  return arr.map((e) => {
    if (e.id === pid) {
      e.children = children;
      return;
    }
    if (e.children && e.children.length !== 0) {
      addChildren(e.children, pid, children);
    }
    return e;
  });
}

// console.log(addChildren(array,381,[{
//   "dicDataKey": 1,
//   "id": 386,
//   "value": "gjhgjkhk",
//   "isActive": true,
//   "isActiveDesc": "是",
//   "haveChild": true,
//   "userName": "admin",
//   "dateTimeDesc": "2022-02-23"
// },{
//   "dicDataKey": 1,
//   "id": 387,
//   "value": "gjhgjkhk",
//   "isActive": true,
//   "isActiveDesc": "是",
//   "haveChild": true,
//   "userName": "admin",
//   "dateTimeDesc": "2022-02-23"
// }]));

// [
//   {
//     children: [],
//     level: 1,
//     paramCode: "appCode",
//     paramType: "String",
//     paramValue: "appCode",
//     parentParamName: "APIname",
//   },
//   {
//     children: [
//       {
//         children: [
//           {
//             children: [],
//             level: 3,
//             paramCode: "work",
//             paramType: "String",
//             paramValue: "work",
//             parentParamName: "APIname.params.params",
//           },
//         ],
//         level: 2,
//         paramCode: "params",
//         paramType: "Map",
//         paramValue: "params",
//         parentParamName: "APIname.params",
//       },
//       {
//         children: [],
//         level: 2,
//         paramCode: "timeStamp",
//         paramType: "String",
//         paramValue: "timeStamp",
//         parentParamName: "APIname.params",
//       },
//     ],
//     level: 1,
//     paramCode: "params",
//     paramType: "List",
//     paramValue: "params",
//     parentParamName: "APIname",
//   },
// ];

const input = [
  { id: "1", code: "appCode", parent: "APIname", type: "String", desc: "1" },
  { id: "2", code: "params", parent: "APIname", type: "Map", desc: "2" },
  // { id: "3", code: "params0", parent: "APIname", type: "List", desc: "2" },
  {
    id: "21",
    code: "timeStamp",
    parent: "APIname.params",
    type: "String",
    desc: "22",
  },
  { id: "22", code: "params", parent: "APIname.params", type: "Map", desc: "" },
  {
    id: "221",
    code: "work",
    parent: "APIname.params.params",
    type: "String",
    desc: "33",
  },
];


function getTree(arr) {
  // 1.分层级
  const newArr = arr
    .map((e) => {
      const p = e.parent.split(".");
      return {
        ...e,
        level: p.length,
      };
    })
    .sort((a, b) => a.level - b.level);
  // 2.处理第一层级
  const root = newArr.filter((e) => e.level === 1);
  // 3.处理其他层级 从第2层开始
  for (let i = 2; i <= newArr[newArr.length - 1].level; i++) {
    // 同一层的数据
    const parmas = newArr.filter((e) => e.level === i);
    loopChildren2Tree(root, parmas, i);
  }
  // 加载子集到树里面
  function loopChildren2Tree(tree, levelParams, level) {
    if (!tree || tree.length === 0) {
      return;
    }
    // 父级要添加的子集level+1和父级的level相等
    if (tree[0].level + 1 === level) {
      tree.forEach((p) => {
        p.children = new Array(levelParams.length);// 最大长度是当前层级的长度
        levelParams.forEach((c) => {
          const p1 = c.parent.split(".");
          const pKey =  p1[p1.length - 1] ;// 孩子绑定的父元素的code或者code+下标
          if (pKey === p.code) {
            c.id = `${p.id}_${c.id}`;
            p.children.push(c);
          }else if(p.type === 'List'){
            const index = pKey.replace(p.code,'');// 获取下标
            if (pKey === `${p.code}${index}`) {
              c.id = `${p.id}_${c.id}`;
              p.children[index] = c;
            }
          }
         
        });
        p.children = p.children.filter(Boolean);// 过滤空项
      });
    } else {
      // 如果不相等，则继续往下找
      tree.forEach((e) => {
        loopChildren2Tree(e.children, levelParams, level);
      });
    }
  }

  return root;
  // console.log("newArr", newArr);
}
console.log(JSON.stringify(getTree(input)));
console.log(getTree(input));
